part of 'table.dart';

/// 时间记录，比如几点到几点干了什么事情
/// 结束时间会作为主键，因为比如范围筛选来看，都会根据结束时间来统计
class Pomo implements Identity {
  /// 唯一id
  @override
  final int id;

  Period period;

  /// 总结这段时间做了什么事情
  String title;
  String summary;

  Pomo({
    required this.period,
    required this.id,
    required this.title,
    required this.summary,
  });

  factory Pomo.fromRaw(Json raw) {
    DateTime parseDateTime(dynamic value) {
      if (value is String) {
        return DateTime.parse(value).toLocal();
      } else if (value is DateTime) {
        return value.toLocal();
      } else {
        throw BugDev('无效的DateTime格式');
      }
    }

    return Pomo(
      id: raw["id"],
      period: Period(parseDateTime(raw["begin"]), parseDateTime(raw["end"])),
      title: raw["title"],
      summary: raw["summary"],
    );
  }

  @override
  Json toJson() => {
        "id": id,
        "begin": period.begin.toString(),
        "end": period.end.toString(),
        "title": title,
        "summary": summary,
      };

  /// 所有内容都相等，那么才算是相等
  @override
  operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other is! Pomo) return false;

    return period == other.period &&
        title == other.title &&
        summary == other.summary &&
        id == other.id;
  }

  @override
  int get hashCode => Object.hash(period, title, summary, id);
}
